/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.DataCache;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/view/set")
public class SetCtrl extends Controller {

    public void index() {

        render("/view/set/setting.jsp");
    }

    public void saveInfo() {

        for (String name : getParaMap().keySet()) {
            Db.update("replace into lis_config set name='" + name + "',value='" + getPara(name) + "'");
            //System.out.println("replace into lis_config set name='" + name + "',value='" + getPara(name) + "'");
        }

        List<Record> list = Db.find("select * from lis_config");
        Map<String, String> map = new HashMap<String, String>();
        if (!list.isEmpty()) {
            for (Record re : list) {
                map.put(re.getStr("name"), re.getStr("value").replace("\\", "\\\\"));
            }
        }
        DataCache.getIntance().setConfig(map);
        try {
           
            DataCache.getIntance().initConfigForHisDb();
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderText("true");
    }

    public void getInfo() {
        List<Record> list = Db.find("select * from lis_config");
        Map<String, String> map = new HashMap<String, String>();
        if (!list.isEmpty()) {
            for (Record re : list) {
                map.put(re.getStr("name"), re.getStr("value").replace("\\", "\\\\"));
            }
        }
        renderJson(map);

    }
    public void getDevices(){
      System.out.println("----------------------");
        renderJson(Db.find("select DEVICEMODEL from lis_deviceset"));
    }
    public void saveReportSet(){
       Db.update("replace into lis_config set name='" + getPara("name") + "',value='" + getPara("value") + "'");
        renderText("true");
    }
    public void getDeviceConfig(){
        Record re = Db.findFirst("select * from lis_config where name = '"+getPara("name")+"'");
        renderText(re==null?"{}":re.getStr("value"));
    }
}
